<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script th:src="@{/static/main.js}"></script>

    <style>
        .cover {
            display: inline-block;
            width: 30px;
            height: 30px;
            position: absolute;
            right: 13px;
            top: 122px;
            z-index: 100;
            cursor: pointer;
        }
    </style>
</head>
<body>
<!--颜醉-->
<!--单个地址修改-->


<div class="container-fluid" id="app" style="margin-top: 10px;margin-bottom: 100px">

    <div class="container" style="margin-top: 10px">

        <div class="row">
            <div class="col-md-12">
                <p style="background-color: cornflowerblue ; color: white; font-size: 30px; padding-left: 10px; border-radius: 4px">
                    <a th:href="@{/usercenter.html}" style="font-size: 30px;color: white;text-decoration: none">个人中心</a>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12 col-md-12">
                <ul class="breadcrumb">
                    <li class="active" style="font-size: 20px">
                        {{address.isDefault==1?'默认&nbsp;&nbsp;&nbsp;':'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'}}{{address.province}}{{address.city}}{{address.area}}{{address.detailAddress}}  {{address.tel}}  {{address.receiver}}
                    </li>
                </ul>
                <div class="thumbnail col-md-4 col-md-offset-4"
                     style="border: 1px solid #cccccc ;border-radius: 5px;box-shadow: 5px 5px 3px 0px #CCCCCC;">
                    <form>
                        <div class="form-group">
                            <label for="provinceId">省</label>
                            <select class="form-control" id="provinceId" placeholder="请选择省份" v-model="provinceId" @change="refreshCity">
                                <option value="-1">请选择省份</option>
                                <option v-for="province in provinceList" :value="province.id" v-text="province.name"></option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="cityId">市</label>
                            <select class="form-control" id="cityId" placeholder="请选择城市" v-model="cityId" @change="refreshCounty">
                                <option value="-1">请选择城市</option>
                                <option v-for="city in cityList" :value="city.id" v-text="city.name"></option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="areaId">区</label>
                            <select class="form-control" id="areaId" placeholder="请选择区域"  v-model="countyId">
                                <option value="-1">请选择区域</option>
                                <option v-for="county in countyList" :value="county.id" v-text="county.name"></option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="detailAddress">详细地址</label>
                            <input type="text" v-model="detailAddress" class="form-control" id="detailAddress" :placeholder="address.detailAddress">
                        </div>

                        <div class="form-group">
                            <label for="tel">手机号</label>
                            <input type="text" v-model="tel" class="form-control" id="tel" :placeholder="address.tel">
                        </div>

                        <div class="form-group">
                            <label for="receiver">收件人姓名</label>
                            <input type="text" v-model="receiver" class="form-control" id="receiver" :placeholder="address.receiver">
                        </div>
                        <div class="form-group">
                            <label for="code">邮编</label>
                            <input type="text" v-model="emailCode" class="form-control" id="code" :placeholder="address.emailCode != '' ? address.emailCode : '请输入邮编'">
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" v-model="isDefault" id="isDefault" > 是否默认
                            </label>
                        </div>

                        <div>
                            <button type="button" class="btn btn-primary" @click.prevent="updateAddress">保存</button>
                            <button type="button" class="btn btn-primary" style="margin-left: 250px" @click.prevent="deleteAddress">删除</button>
                        </div>
                    </form>
                </div>
                <div>
                    <button type="button" class="btn btn-primary" style="margin-left: 100px;" @click.prevent="goBack">返回</button>
                </div>
            </div>
        </div>

    </div>

</div>

<script>

    console.log(".........addresssetting...........")

    let addresssetting = new Vue({
        el: "#app"
        ,
        data: {
            addressId: [[${addressId}]],
            address: '',

            provinceName: '',
            cityName: '',
            areaName: '',

            provinceList:[],
            provinceId:-1,
            cityList:[],
            cityId:-1,
            countyList:[],
            countyId:-1,
            detailAddress:'',
            tel:'',
            receiver:'',
            emailCode:'',
            isDefault: false,
        }
        ,
        methods: {
            initAddressById(){
                let _this = this;
                $.get("/address/selectById?addressId=" + _this.addressId, function (data){
                    _this.address = data;

                    _this.addressId = data.id;
                    _this.provinceName = data.province;
                    _this.cityName = data.city;
                    _this.areaName = data.area;

                    if (data.isDefault == 1) {
                        _this.isDefault = true;
                    }

                })
            }
            ,
            initProvinceList(){  //省 / 直辖市
                let _this = this;

                $.get("/china/queryList?pid=0",function (data) {
                    console.log(data)

                    _this.provinceList = data;

                    //市 区 选择项要置空
                    _this.cityList=[];
                    _this.cityId = -1;
                    _this.countyList = [];
                    _this.countyId = -1;

                })


            }
            ,
            refreshCity(){   //市  / 直辖市的区
                console.log(this.provinceId)

                let _this = this;

                $.get("/china/queryList?pid="+this.provinceId,function (data) {
                    console.log(data)

                    _this.cityList = data;

                    //把县数据置空
                    _this.countyList = [];
                    _this.countyId = -1;
                    _this.cityId = -1;
                })

            }
            ,
            refreshCounty(){   // 县 / -
                console.log(this.cityId)

                let _this = this;

                $.get("/china/queryList?pid="+this.cityId,function (data) {
                    console.log(data)

                    _this.countyList = data;
                })

            }
            ,
            updateAddress() {
                //搜集表单数据
                var _this = this;
                //省市区要id转name
                let provinceName = "";
                for(let item of this.provinceList){
                    if (item.id == this.provinceId){
                        provinceName = item.name;
                    }
                }

                let cityName = "";
                for(let item of this.cityList){
                    if (item.id == this.cityId){
                        cityName = item.name;
                    }
                }

                let countyName = "";
                for(let item of this.countyList){
                    if (item.id == this.countyId){
                        countyName = item.name;
                    }
                }

                let _isDefault = this.isDefault;
                if (_isDefault) {
                    _isDefault = 1;
                } else {
                    _isDefault = 0;
                }

                let _detailAddress = this.detailAddress;
                if (_detailAddress == '') {
                    _detailAddress = this.address.detailAddress;
                }

                let _tel = this.tel;
                if (_tel == '') {
                    _tel = this.address.tel;
                } else {
                    if (!this.isTel(_tel)) {
                        alert("电话格式错误");
                        return false;
                    }
                }

                let _receiver = this.receiver;
                if (_receiver == '') {
                    _receiver = this.address.receiver;
                }

                let _emailCode = this.emailCode;
                if (_emailCode == '') {
                    _emailCode = this.address.emailCode;
                } else {
                    if (!this.isMailCode(_emailCode)) {
                        alert("邮编格式错误");
                        return false;
                    }
                }

                let params = {
                    id:this.addressId,
                    province:provinceName,
                    city:cityName,
                    area:countyName,
                    detailAddress:_detailAddress,
                    tel:_tel,
                    receiver:_receiver,
                    emailCode:_emailCode,
                    isDefault:_isDefault
                }

                console.log(params)

                //数据提交后端接口,修改地址
                $.post("/address/update",params,function () {
                    alert("修改成功");
                    location.href = "/useraddress.html";
                })

            }
            ,
            deleteAddress() {
                $.get("/address/delete?addressId=" + this.addressId,function () {
                    alert("删除成功");
                    location.href = "/useraddress.html";
                })
            }
            ,
            goBack() {
                location.href = "/useraddress.html";
            }
            ,
            isTel(n) {
                var reg=/^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
                return reg.test(n);
            }
            ,
            isMailCode(n) {
                var reg=/[1-9]\d{5}(?!\d)/;
                return reg.test(n);
            }
        }
        ,
        created() {
            this.initAddressById();
            this.initProvinceList();
        }

    });


</script>

</body>
</html>

